namespace ConsoleApp2.input._1016;

using System;
using System.Text.RegularExpressions;
public static class User
{

    // 用户名验证
    public static bool ValidateUsername(string username, out string error)
    {
        error = null;

        if (string.IsNullOrWhiteSpace(username))
        {
            error = "用户名不可为空";
            return false;
        }

        if (username.Length < 3 || username.Length > 15)
        {
            error = "用户名长度必须为 3-15 个字符";
            return false;
        }

        if (!Regex.IsMatch(username, @"^[A-Za-z0-9_]+$"))
        {
            error = "用户名只能包含字母、数字、下划线（_）";
            return false;
        }

        return true;
    }

    // 密码验证
    public static bool ValidatePassword(string password, string confirmPassword, out string error)
    {
        error = null;

        if (string.IsNullOrWhiteSpace(password))
        {
            error = "密码不可为空";
            return false;
        }

        if (password.Length < 6 || password.Length > 20)
        {
            error = "密码长度必须为 6-20 个字符";
            return false;
        }

        if (!Regex.IsMatch(password, @"[A-Z]"))
        {
            error = "密码必须包含至少 1 个大写字母";
            return false;
        }

        if (!Regex.IsMatch(password, @"[a-z]"))
        {
            error = "密码必须包含至少 1 个小写字母";
            return false;
        }

        if (!Regex.IsMatch(password, @"\d"))
        {
            error = "密码必须包含至少 1 个数字";
            return false;
        }

        if (password != confirmPassword)
        {
            error = "密码与确认密码不一致";
            return false;
        }

        return true;
    }

    // 菜单选择验证
    public static bool TryParseMenuSelection(string input, int minOption, int maxOption, out int selection, out string error)
    {
        selection = -1;
        error = null;

        if (string.IsNullOrWhiteSpace(input))
        {
            error = "输入不可为空";
            return false;
        }

        if (!int.TryParse(input.Trim(), out selection))
        {
            error = "请输入有效数字";
            return false;
        }

        if (selection < minOption || selection > maxOption)
        {
            error = $"请输入 {minOption} 到 {maxOption} 之间的数字";
            return false;
        }

        return true;
    }

}

class Contrloller
{
    public bool Login()
    {
        Console.WriteLine("请输入用户名:");
        string name = Console.ReadLine();
        User.ValidateUsername(name, out string error);
        Console.WriteLine(error);
        Console.WriteLine("请输入密码:");
        String password = Console.ReadLine();
        if (!(name == "admin" && "123456Aa".Equals(password)))
            return false;
        else
            return true;
    }
    public bool Register()
    {
        //模拟注册成功
        Console.WriteLine("请输入用户名:");
        string name = Console.ReadLine();
        if (!User.ValidateUsername(name, out string error))
        {
            Console.WriteLine(error);
            return false;
        }
        Console.WriteLine("请输入密码:");
        string password = Console.ReadLine();
        Console.WriteLine("请再次输入密码:");
        string password2 = Console.ReadLine();
        if(!User.ValidatePassword(password, password2,out string error1))
        {
            Console.WriteLine(error1);
            return false;
        }
        // if (!password.Equals(password2))
        // {
        //     Console.WriteLine("两次密码输入不一致，注册失败！");
        //     return false;
        // }
        return true;
    }
}